﻿using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.Unity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Artech.Core.Configuration
{
    public class UnityServiceLocatorData : ServiceLocatorDataBase
    {
        private const string PropertyName4UnityContainerName = "unityContainerName";

        public UnityServiceLocatorData() { }
        public UnityServiceLocatorData(Type type)
            : base(null, type) { }
        public UnityServiceLocatorData(string name, Type type)
            : base(name, type) { }


        [ConfigurationProperty(PropertyName4UnityContainerName, IsRequired = false, DefaultValue = "")]
        public string UnityContainerName
        {
            get { return base[PropertyName4UnityContainerName] as string; }
            set { base[PropertyName4UnityContainerName] = value; }
        }

        public override IEnumerable<TypeRegistration> GetRegistrations(IConfigurationSource configurationSource)
        {
            Expression<Func<IServiceLocator>> newExpression = () => new UnityServiceLocator(configurationSource, this.UnityContainerName);
            TypeRegistration providerTypeRegistration = new TypeRegistration<IServiceLocator>(newExpression)
            {
                Name = this.Name,
                IsPublicName = true,
                Lifetime =  TypeRegistrationLifetime.Singleton
            };

            yield return providerTypeRegistration;
        }
    }
}